<?php
class cookie{
    private $errors = array();
    private $cookieName = null;
    private $cookieData = null;
    private $cookieKey = null;
    private $cookieExpire=0;
    private $cookiePath = '/';
    private $cookieDomain = '';
    private $cookieSecure = false;
    private $cookieHTTPOnly = false;
	public function init($params=array()){
		foreach($params as $key => $var){
			$this->$key=is_array($this->$key)?array_merge($this->$key,$var):$var;
		}
		$this->cookieDomain = $this->getRootDomain();
	}
    public function getCookie($cookieName=null){
        if(is_null($cookieName)){
            $cookieName = $this->cookieName;
        }
        if(isset($_COOKIE[$cookieName])){
            return base64_decode($_COOKIE[$cookieName]);
        } else{
            $this->pushError($cookieName.' not found');
            return false;
        }
    }
    public function createCookie(){
        if(is_null($this->cookieName)){
            $this->pushError('Cookie name was null');
            return false;
        }
        $ret=setcookie($this->cookieName,$this->cookieData,$this->cookieExpire,$this->cookiePath,$this->cookieDomain,$this->cookieSecure,$this->cookieHTTPOnly);
        return $ret;
    }
    public function destroyCookie($cookieName=null){
        if(is_null($cookieName)){
            $cookieName = $this->cookieName;
        }
        $ret=setcookie($cookieName,null,-1,$this->cookiePath,$this->cookieDomain);
        return $ret;
    }
    public function setName($name=null){
        if(!is_null($name)){
            $this->cookieName = $name;
            return $this;
        }
        $this->pushError('Cookie name was null');
        return false;
    }
    public function setValue($value=null, $encrypt=false){
        if(!is_null($value)){
            if(is_array($value)){
                $value = serialize($value);
            }
            $value=base64_encode($value);
            $len = (function_exists('mb_strlen')?mb_strlen($value):strlen($value));
            if($len>4096){
                $this->pushError('Cookie data exceeds 4kb');
                return false;
            }
            $this->cookieData = $value;
            unset($value);
            return $this;
        }
        $this->pushError('Cookie value was empty');
        return false;
    }
    public function setExpire($time=99999999){
        $this->cookieExpire=$this->yum->runtime+$time;
        return $this;
    }
    public function setPath($path='/'){
        $this->cookiePath=$path;
        return $this;
    }
    public function setDomain($domain=null){
        if(!is_null($domain)){
            $this->cookieDomain = $domain;
        }
        return $this;
    }
    public function setSecure($secure=false){
        $this->cookieSecure=(bool)$secure;
        return $this;
    }
    public function setHTTPOnly($httponly=false){
        $this->cookieHTTPOnly = (bool)$httponly;
        return $this;
    }
    private function getRootDomain(){
        $host = $_SERVER['HTTP_HOST'];
        $parts = explode('.', $host);
        if(count($parts)>1){
            $tld = array_pop($parts);
            $domain = array_pop($parts).'.'.$tld;
        } else {
            $domain = array_pop($parts);
        }
        return '.'.$domain;
    }
    private function pushError($error=null){
        if(!is_null($error)){
            $this->errors[] = $error;
        }
        return;
    }
    public function getErrors(){
        return implode("<br />", $this->errors);
    }
}
?>